package name.schooley.umarks.web;

import java.lang.Long;
import java.lang.String;
import java.util.Set;
import javax.validation.Valid;
import name.schooley.umarks.domain.Bookmark;
import name.schooley.umarks.domain.Tag;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

privileged aspect TagController_Roo_Controller {
    
    @RequestMapping(value = "/tag", method = RequestMethod.POST)
    public String TagController.create(@Valid Tag tag, BindingResult result, ModelMap modelMap) {
        if (tag == null) throw new IllegalArgumentException("A tag is required");
        if (result.hasErrors()) {
            modelMap.addAttribute("tag", tag);
            modelMap.addAttribute("bookmarks", Bookmark.findAllBookmarks());
            return "tag/create";
        }
        tag.persist();
        return "redirect:/tag/" + tag.getId();
    }
    
    @RequestMapping(value = "/tag/form", method = RequestMethod.GET)
    public String TagController.createForm(ModelMap modelMap) {
        modelMap.addAttribute("tag", new Tag());
        modelMap.addAttribute("bookmarks", Bookmark.findAllBookmarks());
        return "tag/create";
    }
    
    @RequestMapping(value = "/tag/{id}", method = RequestMethod.GET)
    public String TagController.show(@PathVariable("id") Long id, ModelMap modelMap) {
        if (id == null) throw new IllegalArgumentException("An Identifier is required");
        modelMap.addAttribute("tag", Tag.findTag(id));
        return "tag/show";
    }
    
    @RequestMapping(value = "/tag", method = RequestMethod.GET)
    public String TagController.list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            modelMap.addAttribute("tags", Tag.findTagEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
            float nrOfPages = (float) Tag.countTags() / sizeNo;
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            modelMap.addAttribute("tags", Tag.findAllTags());
        }
        return "tag/list";
    }
    
    @RequestMapping(method = RequestMethod.PUT)
    public String TagController.update(@Valid Tag tag, BindingResult result, ModelMap modelMap) {
        if (tag == null) throw new IllegalArgumentException("A tag is required");
        if (result.hasErrors()) {
            modelMap.addAttribute("tag", tag);
            modelMap.addAttribute("bookmarks", Bookmark.findAllBookmarks());
            return "tag/update";
        }
        tag.merge();
        return "redirect:/tag/" + tag.getId();
    }
    
    @RequestMapping(value = "/tag/{id}/form", method = RequestMethod.GET)
    public String TagController.updateForm(@PathVariable("id") Long id, ModelMap modelMap) {
        if (id == null) throw new IllegalArgumentException("An Identifier is required");
        modelMap.addAttribute("tag", Tag.findTag(id));
        modelMap.addAttribute("bookmarks", Bookmark.findAllBookmarks());
        return "tag/update";
    }
    
    @RequestMapping(value = "/tag/{id}", method = RequestMethod.DELETE)
    public String TagController.delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {
        if (id == null) throw new IllegalArgumentException("An Identifier is required");
        Tag.findTag(id).remove();
        return "redirect:/tag?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());
    }
    
    @RequestMapping(value = "find/ByBookmarks/form", method = RequestMethod.GET)
    public String TagController.findTagsByBookmarksForm(ModelMap modelMap) {
        modelMap.addAttribute("bookmarks", Bookmark.findAllBookmarks());
        return "tag/findTagsByBookmarks";
    }
    
    @RequestMapping(value = "find/ByBookmarks", method = RequestMethod.GET)
    public String TagController.findTagsByBookmarks(@RequestParam("bookmarks") Set<Bookmark> bookmarks, ModelMap modelMap) {
        if (bookmarks == null) throw new IllegalArgumentException("A Bookmarks is required.");
        modelMap.addAttribute("tags", Tag.findTagsByBookmarks(bookmarks).getResultList());
        return "tag/list";
    }
    
    @RequestMapping(value = "find/ByTag/form", method = RequestMethod.GET)
    public String TagController.findTagsByTagForm(ModelMap modelMap) {
        return "tag/findTagsByTag";
    }
    
    @RequestMapping(value = "find/ByTag", method = RequestMethod.GET)
    public String TagController.findTagsByTag(@RequestParam("tag") String tag, ModelMap modelMap) {
        if (tag == null || tag.length() == 0) throw new IllegalArgumentException("A Tag is required.");
        modelMap.addAttribute("tags", Tag.findTagsByTag(tag).getResultList());
        return "tag/list";
    }
    
}
